/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * This example demonstrates how to send data to another GIMI-client.
 * 
 * Check other files for more advanced tutorials on GIMI-features.
 */

#include "gimi.h"

int main(int argc, char **argv) {
  
  // Initialize debug printing
  debugInit();
  debugSetGlobalDebugLvl(6);
  
  std::string targetName = "SimpleReceiver";
  
  int messageCount = 0;
  
  // Create the GIMI-object.
  dPrint(1, "Create object.");
  gimi::GIMI gi;
  
  // Connect to tcphub on ASRobo
  if (!gi.connectToHub("asrobo.hut.fi", 50002, "SimpleSender")) {
    dPrint(1, "Could not connect to tcpHub. Are connection parameters correct?");
    return -1;
  }
  
  // Create dataobject for sending.
  TGIMI_PROTOCOL_IMAGESERVER_CTRL imageServerControlPacket;
  
  // Sender loop that ends after sending 2 messages.
  while (messageCount < 2) {
    // Set shutter speed in control packet to a new value.
    imageServerControlPacket.shutter = 100 + messageCount;
    
    // Make a more generic pointer to the data.
    char* data = reinterpret_cast< char* >(&imageServerControlPacket);
    int datalength = sizeof(TGIMI_PROTOCOL_IMAGESERVER_CTRL);
    
    // Send packet. Note the last two parameters. First is the datatype of the packet (majortype) and 
    // the second minortype, which can be used to distinguish for example two webcams that both send
    // data with the same datatype.
    int sendResult = gi.send(targetName, data, datalength, GIMI_PROTOCOL_IMAGESERVER_CTRL, 0);
 
    if (sendResult == GIMI_OK) {
      printf("Successfully sent message!\n");
      
      // Wait 2 seconds before continuing.
      ownSleep_ms(2000);
    } else {
      dPrint(1, "Error %d (%s) occurred, quitting.", sendResult, gimi::getGimiErrorString(sendResult).c_str());
    }
    
    ++messageCount;
  }
  
  // Closes logfile in case if it was used. (debug printing)
  debugDeinit();
  
  return 0;
}
